mod api;
mod service;
mod repository;
mod model;
mod config;

use crate::api::{get_environment_data, add_environment_data, extract_text};
use crate::service::EnvironmentService;
use crate::repository::EnvironmentRepository;
use crate::model::{EnvironmentData, CreateEnvironmentData};
use crate::config::init_db;
use actix_web::{web, App, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dotenv::dotenv().ok();
    
    let pool = config::database::init_db()
        .await
        .expect("Failed to initialize database");
    let repository = EnvironmentRepository::new(pool);
    let service = web::Data::new(EnvironmentService::new(repository));

    println!("服务器启动在 http://0.0.0.0:5000");
    
    HttpServer::new(move || {
        App::new()
            .app_data(service.clone())
            .route("/environment", web::get().to(get_environment_data))
            .route("/environment", web::post().to(add_environment_data))
            .route("/ocr", web::post().to(extract_text))
    })
    .bind("0.0.0.0:5000")?
    .run()
    .await
}